<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">班费管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加学生</el-breadcrumb-item>
    </el-breadcrumb>


    <el-divider></el-divider>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="ruleForm.nickname"></el-input>
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="电子邮箱" prop="email">
        <el-input v-model="ruleForm.email"></el-input>
      </el-form-item>
      <el-form-item label="是否启用" prop="enable">
        <el-switch
            v-model="ruleForm.enable"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#aaa">
        </el-switch>
      </el-form-item>
      <el-form-item label="角色" prop="roleIds">
          <el-select v-model="ruleForm.roleIds" multiple placeholder="请选择">
            <el-option
                v-for="item in roleListOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="班级" prop="classId">
        <el-select v-model="ruleForm.classId" clearable placeholder="请选择">
          <el-option
              v-for="item in classListOptions"
              :key="item.id"
              :label="item.className"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>


<script>
import axios from "axios";

export default {
  data() {
    return {
      roleListOptions: [{
        id: '选项1',
        name: '黄金糕'
      }, {
        id: '选项2',
        name: '双皮奶'
      }],
      classListOptions: [{
        id: '选项1',
        name: '黄金糕'
      }, {
        id: '选项2',
        name: '双皮奶'
      }],
      ruleForm: {
        username:'',
        password:'',
        nickname:'',
        phone:'',
        email:'',
        enable:1,
        roleIds: [],
        classId:'',
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
        nickname: [
          { required: true, message: '请输入昵称', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
        phone:[
          { required: true, message: '请输入电话号码', trigger: 'blur' },
          { min: 8, max: 15, message: '长度在 8 到 15 个字符', trigger: 'blur' }
        ],
        email:[
          { required: true, message: '请输入电子邮箱', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 30 个字符', trigger: 'blur' }
        ],
        roleIds:[
          { required: true, message: '至少选择一个角色', trigger: 'blur' },
        ],
        classId:[
          { required: true, message: '请选择一个班级', trigger: 'blur' },
        ],
      }
    };
  },
  methods: {
    loadRoleList(){
      console.log("loadRoleList")
      let url = "http://127.0.0.1:8080/roles";
      axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        this.roleListOptions = responseBody.data;
      })
    },

    loadClassList(){
      console.log("loadClassList")
      let url = "http://127.0.0.1:8080/class";
      axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        this.classListOptions = responseBody.data;
      })
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "http://127.0.0.1:8080/user/add-new";
          console.log('url = '+url);
          let formData = this.qs.stringify(this.ruleForm,{arrayFormat:'repeat'});
          console.log('formData = '+formData);
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url,formData).then((response) => {
            let resopnseBody = response.data;
            console.log("resopnseBody = " );
            console.log(resopnseBody);
            if (resopnseBody.state == 20000){
              this.$message({
                message: "添加成功",
                type: 'success'
              });
              this.resetForm(formName);
            }else{
              this.$message.error(resopnseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },


  mounted(){
    this.loadRoleList();
    this.loadClassList();
  }
}
</script>
